//
//  WclAlterView.m
//  YZMicroStock
//
//  Created by apple on 2017/10/31.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "WclAlterView.h"

typedef void(^ConfirmHandle)();

@interface WclAlterView()

@property (nonatomic, strong) UIView *viewAlpha;

@property (nonatomic, copy)ConfirmHandle confirmHandle;


@end

@implementation WclAlterView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/




+ (instancetype)altertViewWithTitle:(NSString *)title array:(NSArray *)array confirmHandle:(void(^)())confirmHandle{
    WclAlterView *alterView = [[self alloc] init];
    alterView.confirmHandle = [confirmHandle copy];
    [YZMSetBorderAndCornerRadius  setView:alterView borderWidth:0 borderColor:nil cornerRadius:4.0f];
    alterView.backgroundColor = [UIColor whiteColor];
    if(title.length == 0){
        title = @"提示";
    }
    UILabel *labelTitle = [[UILabel alloc] init];
    labelTitle.text  = title;
    labelTitle.textAlignment = NSTextAlignmentCenter;
    [alterView addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(alterView.mas_top).with.offset(15);
        make.left.right.equalTo(alterView);
    }];
    
    for(NSInteger i = 0; i < array.count; i++){
        NSDictionary *dic = array[i];
        UILabel *labelLeft = [[UILabel alloc] init];
        labelLeft.font  = [UIFont systemFontOfSize:14.0];
        labelLeft.textColor = [YZMAppStyle colorForGrayText];
        labelLeft.tag = 100 + i;
        labelLeft.text = dic.allKeys[0];
        [alterView addSubview:labelLeft];
        if(100 + i > 100){
            UILabel *labelLeftPri = [alterView viewWithTag:100+i -1];
            [labelLeft mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(alterView.mas_left).with.offset(15);
                make.top.equalTo(labelLeftPri.mas_bottom).with.offset(10);
            }];
        }else{
            [labelLeft mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(alterView.mas_left).with.offset(15);
                make.top.equalTo(labelTitle.mas_bottom).with.offset(10);
            }];
        }
        
        
        UILabel *labelRight = [[UILabel alloc] init];
        labelRight.tag = 1000 + i;
        labelRight.font  = [UIFont systemFontOfSize:14.0];
        labelRight.textColor = [YZMAppStyle colorForGrayText];
        labelRight.text = dic[dic.allKeys[0]];
        [alterView addSubview:labelRight];
        if(1000 + i > 1000){
            UILabel *labelRightPri = [alterView viewWithTag:1000 + i - 1];
            [labelRight mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(alterView.mas_right).with.offset(-15);
                make.top.equalTo(labelRightPri.mas_bottom).with.offset(10);
            }];
        }else{
            [labelRight mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(alterView.mas_right).with.offset(-15);
                make.top.equalTo(labelTitle.mas_bottom).with.offset(10);
            }];
        }
        
        if(i == array.count - 1){
            UILabel *labelLast = [alterView viewWithTag:100 + i];
            UIView *viewOption = [[UIView alloc] init];
            [alterView addSubview:viewOption];
            [viewOption mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(labelLast.mas_bottom).with.offset(10);
                make.left.right.equalTo(alterView);
                make.height.equalTo(@40);
                make.bottom.equalTo(alterView.mas_bottom);
            }];
            UIView *ivLine = [[UIView alloc] init];
            ivLine.backgroundColor = [YZMAppStyle colorForLine];
            [viewOption addSubview:ivLine];
            [ivLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.equalTo(viewOption);
                make.height.equalTo(@1);
            }];
            
            UIButton *btnCancle = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnCancle setTitleColor:[YZMAppStyle colorForRiseText] forState:UIControlStateNormal];
            [btnCancle setTitle:@"取消" forState:UIControlStateNormal];
            [btnCancle addTarget:alterView action:@selector(cancleDo:) forControlEvents:UIControlEventTouchUpInside];
            [viewOption addSubview:btnCancle];
            [btnCancle mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(viewOption.mas_left);
                make.top.equalTo(viewOption.mas_top);
                make.height.equalTo(viewOption.mas_height);
                make.width.equalTo(@(ScreenWidth / 2.0));
            }];
            
            UIView *viewLine = [[UIView alloc] init];
            viewLine.backgroundColor = [YZMAppStyle colorForLine];
            [btnCancle addSubview:viewLine];
            [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(btnCancle);
                make.right.equalTo(btnCancle.mas_right).with.offset(-1);
                make.width.equalTo(@1);
            }];
            
            UIButton *btnSubmit = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnSubmit setTitleColor:[YZMAppStyle colorForRiseText] forState:UIControlStateNormal];
            [btnSubmit setTitle:@"确定" forState:UIControlStateNormal];
            [btnSubmit addTarget:alterView action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
            [viewOption addSubview:btnSubmit];
            [btnSubmit mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(viewOption);
                make.top.equalTo(viewOption);
                make.height.equalTo(viewOption.mas_height);
                make.width.equalTo(@(ScreenWidth / 2.0));
            }];
        }
        
    }
    
    return alterView;
}

- (void)cancleDo:(UIButton *)btn{
    [self hiddenView];
}

- (void)submit{
    [self hiddenView];
    if (self.confirmHandle) {
        self.confirmHandle();
    }
}

- (void)hiddenView{
    [UIView animateWithDuration:0.25 animations:^{
        self.viewAlpha.alpha = 0;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self.viewAlpha removeFromSuperview];
        [self removeFromSuperview];
    }];
}

- (void)show{
    UIWindow *wd = [UIApplication sharedApplication].keyWindow;
    [wd addSubview:self.viewAlpha];
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.viewAlpha mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(wd).insets(insets);
    }];
    [wd addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(wd.mas_centerY);
        make.left.equalTo(wd.mas_left).with.offset(15);
        make.right.equalTo(wd.mas_right).with.offset(-15);
    }];
}

- (UIView *)viewAlpha{
    if (!_viewAlpha) {
        _viewAlpha = [[UIView alloc] init];
        _viewAlpha.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    }
    return _viewAlpha;
}


@end
